﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ROrganizer.Client;
using ROrganizer.UI.Controllers;
using ROrganizer.UI.Forms.Core;
using ROrganizer.UI.Properties;

namespace ROrganizer.UI.Forms
{
    internal partial class CredentialsForm : Form
    {
        private readonly CredentialsFormController controller;

        public CredentialsForm(bool allowCreateAccount)
        {
            this.controller = new CredentialsFormController();
            this.InitializeComponent();

            this.pbMain.Image = Resources.Keepassx;
            this.llblCreateAccount.Visible = allowCreateAccount;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.btnLogin.Enabled = this.controller.CanLogin();
        }

        public HashCredentials GetCredentials()
        {
            return this.controller.GetCredentials();
        }

        private void OnUsernameChanged(object sender, EventArgs e)
        {
            this.controller.Username = this.tbUsername.Text;
            this.btnLogin.Enabled = this.controller.CanLogin();
        }

        private void OnPasswordChanged(object sender, EventArgs e)
        {
            this.controller.Password = this.tbPassword.Text;
            this.btnLogin.Enabled = this.controller.CanLogin();
        }

        private void OnLogin(object sender, EventArgs e)
        {
            this.ExecuteAndWatch(this.Login);
        }

        private void Login()
        {
            this.controller.Login();
            this.btnLogin.Enabled = this.controller.CanLogin();
            this.DialogResult = DialogResult.OK;
        }

        private void OnCreateAccount(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.ExecuteAndWatch(this.controller.CreateAccount, this);
        }
    }
}
